﻿/*-----------------------------------------------
// File: BattleSkillRelease.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Battle.View
{
    public class BattleSkillRelease
    {
        public string HSRId;                   // HSRId
        public string HSId;                    // HSId
        public int shakeCameraType;
        public string HeroId {
            get {
                return battleSkill.heroId;
            }
        }
        public string HeroUid {
            get {
                return battleSkill.heroUid;
            }
        }
        public SkillType SkillType {
            get {
                return battleSkill.skillType;
            }
        }
        public float releaseTime;             // Release Time
        public float beHitTime;

        public ParticleData beHitParticle_Target = null;           // 被击特效，挂载到目标
        public ParticleData beHitparticle_Owner = null;          // 被击特效，以自身偏移
        public ParticleData beHitParticle_Ground = null;                   // 被击特效，地面

        public string beHitAudio;                                   // 被击声音

        public ActorSpecialState causeSpecialState = ActorSpecialState.NONE;
        public int exportType = 0;

        public bool haveProjectile {
            get { return projData != null; }
        }

        public ProjectileData projData = null;


        List<string> immediateBuffId = new List<string>();

        BattleSkill battleSkill = null;

        public BattleCharacter ownBC {
            get { return battleSkill.GetOwnBC(); }
        }

        public bool IsLastRelease() {
            return this == battleSkill.GetLastSkillRelease();
        }
        public BattleSkillRelease(Dict.DataModel.TableSkillCast dict, BattleSkill bs) {
            battleSkill = bs;
            SetData(dict);
            isLoadComplete = false;
        }
        void SetData(Dict.DataModel.TableSkillCast dict) {
            HSRId = dict.SkillCastId;
            HSId = dict.SkillId;
            releaseTime = (float)dict.CastTime;
            shakeCameraType = dict.ShakeScreen;
            causeSpecialState = (ActorSpecialState)dict.CauseSpeState;
            beHitTime = (float)dict.BehitStiffTime;
            exportType = dict.ExportType;
            if (!string.IsNullOrEmpty(dict.BehitEffectEnemy))
                beHitParticle_Target = ParticleData.CreateParticleData(dict.BehitEffectEnemy, dict.BehitEffBone);
            if (!string.IsNullOrEmpty(dict.BehitEffSelf))
                beHitparticle_Owner = ParticleData.CreateParticleData(dict.BehitEffSelf, TableConvertUtil.String2Vector3(dict.BehitEffOffset));
            if (!string.IsNullOrEmpty(dict.GroundEffect))
                beHitParticle_Ground = ParticleData.CreateParticleData(dict.GroundEffect);

            beHitAudio = dict.BehitAudio;

            if (!string.IsNullOrEmpty(dict.ProjectId)) {
                projData = ProjectileData.CreateProjectileData(dict.ProjectId, (float)dict.FlyTime, (ProjectileFlyType)dict.ProjFlyType, TableConvertUtil.String2Vector3(dict.ProjPos), dict.ProjTargetBone);
            }

            immediateBuffId = new List<string>();
            foreach (var buf in dict.BufferId_array) {
                if (!string.IsNullOrEmpty(buf)) {
                    var dictBuff = Dict.Blo.DictBuffBlo.GetTableBuff(buf);
                    if (dictBuff.BuffClass != (int)Battle.Engine.BuffClass.DelayCreate &&
                        dictBuff.TotalTime <= 0 &&
                        dictBuff.TotalRound <= 0 &&
                        dictBuff.AttCount <= 0 &&
                        dictBuff.DefCount <= 0 &&
                        !string.IsNullOrEmpty(dictBuff.EffectExport))
                    {
                        if (dictBuff.BuffClass == (int)Battle.Engine.BuffClass.Random)
                        {
                            var arr = dictBuff.Param.Split(',');
                            foreach (var _buf in arr)
                            {
                                var _dictBuff = Dict.Blo.DictBuffBlo.GetTableBuff(_buf);
                                if (_dictBuff.BuffClass != (int)Battle.Engine.BuffClass.DelayCreate &&
                                    _dictBuff.BuffClass != (int)Battle.Engine.BuffClass.Random &&
                                    _dictBuff.TotalTime <= 0 &&
                                    _dictBuff.TotalRound <= 0 &&
                                    _dictBuff.AttCount <= 0 &&
                                    _dictBuff.DefCount <= 0 &&
                                    !string.IsNullOrEmpty(_dictBuff.EffectExport))
                                {
                                    immediateBuffId.Add(_buf);
                                }
                            }
                        }
                        else {
                            immediateBuffId.Add(dictBuff.Id);
                        }
                    }
                }
            }

        }

        bool isLoadComplete = false;
        public bool LoadComplete() { return isLoadComplete; }
        public IEnumerator LoadResource() {
            int taskCount = 0;
            if (beHitParticle_Ground != null) {
                ParticleFactory.RegistParticle(beHitParticle_Ground.resUrl, () => { taskCount--; });
                taskCount++;
            }
            if (beHitparticle_Owner != null) {
                ParticleFactory.RegistParticle(beHitparticle_Owner.resUrl, () => { taskCount--; });
                taskCount++;
            }
            if (beHitParticle_Target != null) {
                ParticleFactory.RegistParticle(beHitParticle_Target.resUrl, () => { taskCount--; });
                taskCount++;
            }
            if (haveProjectile) {
                ParticleFactory.RegistParticle(projData.resUrl, () => { taskCount--; });
                taskCount++;
            }
            if(!string.IsNullOrEmpty(beHitAudio.Trim()))
            {
                BattleViewCtrl.GetInstance().StartCoroutine(
                    AudioManager.Instance.LoadAudioResource(beHitAudio, () => { taskCount--; })
                    );
                taskCount++;
            }

            //foreach (var str in immediateBuffId) {
            //    var pd = ParticleData.CreateParticleData(str);
            //    ParticleFactory.RegistParticle(pd.resUrl, () => { taskCount--; });
            //    taskCount++;
            //}

            while (taskCount > 0) {
                yield return null;
            }
            isLoadComplete = true;
        }

        public void UnloadResource() {
            if (beHitParticle_Ground != null)
            {
                ParticleFactory.UnRegistParticle(beHitParticle_Ground.resUrl);
            }
            if (beHitparticle_Owner != null)
            {
                ParticleFactory.UnRegistParticle(beHitparticle_Owner.resUrl);
            }
            if (beHitParticle_Target != null)
            {
                ParticleFactory.UnRegistParticle(beHitParticle_Target.resUrl);
            }
            if (haveProjectile)
            {
                ParticleFactory.UnRegistParticle(projData.resUrl);
            }
            if( !string.IsNullOrEmpty( beHitAudio.Trim() ) )
            {
                AudioManager.Instance.UnloadAudioResource( beHitAudio );
            }
        }


        public static string GetComboGuideFloatReleaseId()
        {
            var dh = Dict.Blo.DictHeroBlo.GetTableHero(CGuideBattleViewCtrl.kiritoHeroId);
            var dscs = Dict.Blo.DictHeroSkillBlo.GetTableSkillCastByHSId(dh.SkillId1);
            foreach (var dsc in dscs)
            {
                if (dsc.CauseSpeState == (int)ActorSpecialState.FLOOR)
                {
                    return dsc.SkillCastId;
                }
            }
            return null;
        }

        public static string GetComboGuideFallReleaseId()
        {
            var dh = Dict.Blo.DictHeroBlo.GetTableHero(CGuideBattleViewCtrl.kiritoHeroId);
            var dscs = Dict.Blo.DictHeroSkillBlo.GetTableSkillCastByHSId(dh.SkillId2);
            foreach (var dsc in dscs)
            {
                if (dsc.CauseSpeState == (int)ActorSpecialState.GROUND)
                {
                    return dsc.SkillCastId;
                }
            }
            return null;
        }
    }
}
